package com.emep.changzhi.analyse.service.basics;

import com.emep.changzhi.analyse.entity.basics.SysDepartmentInfo;
import com.emep.changzhi.analyse.entity.basics.SysLeadershipInfo;
import com.emep.changzhi.analyse.repository.basics.LeaderRepository;
import com.emep.changzhi.analyse.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
 * @author: LFY
 * @version:V1.0
 * @Description:
 * @Date: 2018/9/3 10:55
 */

@Service
public class LeaderService {

    @Autowired
    LeaderRepository leaderRepository;


    public Page<SysLeadershipInfo> find(int pageIndex, int pageSize, Map<String, Object> params) {
        Page<SysLeadershipInfo> leaderPage = leaderRepository.findAll(new LeaderSpec(params),
                PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC, "name"));
        return leaderPage;
    }

    public SysLeadershipInfo getLeader(Long leaderId) {
        return leaderRepository.findByLeaderId(leaderId);
    }

    public boolean add(SysLeadershipInfo leader) {
        List<SysLeadershipInfo> infos =leaderRepository.findAllByNameAndIsDelete(leader.getName(), Constant.RESULT_DELETECODE_NO);
        if(infos.isEmpty()){
            leaderRepository.save(leader);
            return true;
        }
        return false;
    }

    private class LeaderSpec implements Specification<SysLeadershipInfo> {
        private Map<String, Object> params = new HashMap<String, Object>();

        public LeaderSpec(Map<String, Object> params) {
            this.params = params;
        }

        @Override
        public Predicate toPredicate(Root<SysLeadershipInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> lstPredicates = new ArrayList<Predicate>();
            if (params.containsKey("isDelete")) {
                lstPredicates.add(cb.like(root.get("isDelete").as(String.class), params.get("isDelete").toString()));
            }
            if (params.containsKey("epaId")) {
                lstPredicates.add(cb.like(root.get("epaId").as(String.class), params.get("epaId").toString()));
            }
            if (params.containsKey("name")) {
                lstPredicates.add(cb.like(root.get("name").as(String.class), params.get("name").toString()));
            }
            if (params.containsKey("phone")) {
                lstPredicates.add(cb.like(root.get("phone").as(String.class), params.get("phone").toString()));
            }
            Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
            return cb.and(lstPredicates.toArray(arrayPredicates));
        }
    }

    public boolean deleteLeader(SysLeadershipInfo sysLeadershipInfo){
        SysLeadershipInfo sysLeadershipInfo1 = leaderRepository.save(sysLeadershipInfo);
        if(sysLeadershipInfo1 != null){
            return true;
        }else {
            return false;
        }
    }

    public boolean updateLeader(SysLeadershipInfo leader){
        List<SysLeadershipInfo> infos =leaderRepository.findAllByNameAndLeaderIdNotAndIsDelete(leader.getName(),leader.getLeaderId(),Constant.RESULT_DELETECODE_NO);
        if(infos.isEmpty()){
            leaderRepository.save(leader);
            return true;
        }
        return false;

   }
}